﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BingSpatialDataServiceExcelAddin.BSDSTools.FileTools
{
    public class ColumnHeader : List<ColumnDefinition>
    {
        #region Constructor

        ///<summary>
        /// Constructor
        ///</summary>
        public ColumnHeader()
        {
        }

        #endregion

        #region Public Methods

        public IList<string> GetColumnNames()
        {
            IList<string> names = new List<string>();
            foreach (ColumnDefinition c in this)
            {
                names.Add(c.Name);
            }   
            return names;
        }

        public IList<string> GetColumnNames(bool includeTypeNames)
        {
            IList<string> names = new List<string>();
            if (includeTypeNames)
            {
                foreach (ColumnDefinition c in this)
                {
                    if (!string.IsNullOrEmpty(c.TypeName))
                    {
                        if (c.IsPrimaryKey)
                        {
                            names.Add(string.Format("{0}({1},primaryKey)", c.Name, c.TypeName));
                        }
                        else
                        {
                            names.Add(string.Format("{0}({1})", c.Name, c.TypeName));
                        }
                    }
                    else
                    {
                        if (c.IsPrimaryKey)
                        {
                            names.Add(c.Name + "(primaryKey)");
                        }
                        else
                        {
                            names.Add(c.Name);
                        }
                    }
                }  
            }
            else
            {
                foreach (ColumnDefinition c in this)
                {
                    names.Add(c.Name);
                }             
            }

            return names;
        }

        public ColumnDefinition GetColumnDefinition(int index)
        {
            if (this.Count > index)
            {
                int i = 0;

                foreach(ColumnDefinition cd in this){
                    if(i == index){
                        return cd;
                    }
                    i++;
                }
            }

            return null;
        }

        public int GetIndex(string coloumnName)
        {
            if (!string.IsNullOrEmpty(coloumnName))
            {
                for (int i = 0; i < this.Count; i++)
                {
                    if (string.Compare(this[i].Name, coloumnName, true) == 0)
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        public int GetPrimaryKeyIndex()
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].IsPrimaryKey)
                {
                    return i;
                }
            }

            return -1;
        }

        public string[] GetTypeNames()
        {
            string[] types = new string[this.Count];

            int i = 0;

            foreach (ColumnDefinition cd in this)
            {
                types[i] = cd.TypeName;
                i++;
            }

            return types;
        }

        public bool HasPrimaryKey()
        {
            foreach (ColumnDefinition cd in this)
            {
                if (cd.IsPrimaryKey)
                {
                    return true;
                }
            }

            return false;
        }

        public bool ContainsName(string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                var y = (from x in this
                         where string.Compare(x.Name, name, true) == 0
                         select x).FirstOrDefault();

                return (y != null && !string.IsNullOrEmpty(y.Name));
            }

            return false;
        }

        #endregion
    }
}
